package com.bilibili.juc.cf.one;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class CompletableFutureOfOrTimeout3 {
    public static void main(String[] args) {
        // 4.10. 超时和取消
        CompletableFuture<String> timeoutFuture = CompletableFuture
                .supplyAsync(() -> {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    return "正常返回结果没有超时";
                })
                .orTimeout(3, TimeUnit.SECONDS)
                .exceptionally(ex -> {
                    ex.printStackTrace();
                    System.out.println("超时异常: " + ex.getMessage());
                    return "超时异常设置的返回值";
                });

        System.out.println("finally result==> " + timeoutFuture.join());
    }
}
/*
finally result==> 正常返回结果没有超时
 */
